<template>
	<view class="custom_news_list_tools">
		<view class="tools" v-if="!loading">
			<view class="tools_item">
				<custom-fav-button :item="fav"></custom-fav-button>
				<custom-good-button :item="good"></custom-good-button>
				<view class="last_users" v-if="good.lastGood">
					<navigator :url="'../index/goodAvatars?id=' + item.newsId">
						<custom-avatar-group :item="{ data: good.lastGood, max: 9999, count: good.count }"></custom-avatar-group>
						<view class="text">{{ i18n('人点赞') }}</view>
					</navigator>
				</view>
			</view>
			<view class="tools_item"><view class="item_button iconfont icon-share" @click="shareHandle"></view></view>
		</view>
		<uni-load-more v-if="loading" :status="'loading'"></uni-load-more>
	</view>
</template>

<script>
export default {
	name: 'custom-news-list-tools',
	props: {
		item: {
			type: Object,
			default() {
				return {};
			}
		}
	},
	data() {
		return {
			loading: false,
			good: {},
			fav: {},
			follower: {
				data: []
			}
		};
	},
	created() {
		this.getToolsData();
	},
	methods: {
		shareHandle() {
			uni.navigateTo({
				url:'../../pages/index/shareAvatars?id='+this.$props.item.newsId
			})
		},
		i18n(v) {
			return this.$t('index')[v];
		},
		getToolsData() {
			if (!this.loading) {
				this.loading = true;
				Promise.all([this.getGoodStatus(), this.getFavStatus()])
					.then(res => {
						console.log(res);
					})
					.catch(err => {
						console.log(err);
					})
					.finally(() => (this.loading = false));
			}
		},
		getGoodStatus() {
			let that = this;
			return new Promise((resolve, reject) => {
				that.$store
					.dispatch('getGoodStatus', {
						newsId: that.$props.item.newsId,
						userId: getApp().globalData.userInfo.userId
					})
					.then(res => {
						console.log(res);
						that.good = res.data[0];
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject();
					})
					.finally(() => {});
			});
		},
		getFavStatus() {
			let that = this;
			return new Promise((resolve, reject) => {
				that.$store
					.dispatch('getFavStatus', {
						newsId: that.$props.item.newsId,
						userId: getApp().globalData.userInfo.userId
					})
					.then(res => {
						console.log(res);
						that.fav = res.data[0];
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject();
					})
					.finally(() => {});
			});
		}
	}
};
</script>

<style lang="less">
.custom_news_list_tools {
	margin: 0 20rpx;

	.tools {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		.tools_item {
			display: flex;
			.last_users {
				navigator {
					display: flex;
					align-items: center;
					.text {
						font-size: 24rpx;
						margin: 0 0 0 20rpx;
					}
				}
			}
			.item_button {
				&.iconfont {
					font-size: 48rpx;
				}
			}
			&:first-child {
				width: 80%;
				.item_button {
					margin: 0 10rpx 0 0;
				}
			}
			&:last-child {
				display: block;
				text-align: right;
				width: 20%;
				.item_button {
					margin: 0 0 0 10rpx;
				}
			}
		}
	}
}
</style>
